package System_of_Students;

import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    private static final int ADD_STUDENT = 1;
    private static final int REMOVE_STUDENT = 2;
    private static final int CHANGE_STUDENT = 3;
    private static final int SEARCH_STUDENT = 4;
    private static final int RANK_STUDENT = 5;
    private static final int ANALYSE_STUDENT = 6;
    private static final int PRINT_STUDENT = 7;
    private static final int EXIT = 8;

    public static void main(String[] args) {
        System.out.println("-------------欢迎来到学生成绩管理系统----------------");
        ArrayList<Student> list = new ArrayList<>();
        init(list);
    }

    public static void init(ArrayList<Student> list) {
        System.out.println("1：添加学生");
        System.out.println("2：删除学生");
        System.out.println("3：修改学生");
        System.out.println("4：查询学生");
        System.out.println("5：学生成绩排名");
        System.out.println("6：班级成绩分析");
        System.out.println("7：输出学生信息表");
        System.out.println("8：退出");
        System.out.println("请输入您的选择:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        switch (n) {
            case ADD_STUDENT:
                add(list);
            case REMOVE_STUDENT:
                remove(list);
            case CHANGE_STUDENT:
                change(list);
            case SEARCH_STUDENT:
                search(list);
            case RANK_STUDENT:
                rank(list);
            case ANALYSE_STUDENT:
                analyse(list);
            case PRINT_STUDENT:
                printStudent(list);
            case EXIT:
                System.exit(0);
        }
    }

    public static void add(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student s = new Student();
        System.out.println("请输入学生学号");
        String id = sc.next();
        int index = getindex(list, id);
        if (index != -1) {
            System.out.println("输入学号重复，请重新输入");
            add(list);
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生班级");
        String classroom = sc.next();
        System.out.println("请输入学生数学成绩");
        int math = sc.nextInt();
        System.out.println("请输入学生英语成绩");
        int English = sc.nextInt();
        System.out.println("请输入学生计算机成绩");
        int Computer = sc.nextInt();
        double average = (math + English + Computer) / 3.0;
        s.setId(id);
        s.setName(name);
        s.setClassroom(classroom);
        s.setMath(math);
        s.setEnglish(English);
        s.setComputer(Computer);
        s.setAverage(Double.parseDouble(String.format("%.2f", average)));
        list.add(s);
        System.out.println("是否继续添加(1为是2为否)");
        int n = sc.nextInt();
        if (n == 1) {
            add(list);
        } else {
            init(list);
        }
    }

    public static void remove(ArrayList<Student> list) {
        System.out.println("请选择需要删除的学生(学号)");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        int index = getindex(list, id);
        if (index == -1) {
            System.out.println("输入学号有误，请重新输入");
            remove(list);
        } else {
            list.remove(index);
        }
        System.out.println("是否继续删除(1为是2为否)");
        int n = sc.nextInt();
        if (n == 1) {
            remove(list);
        } else {
            init(list);
        }
    }

    public static void change(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择需要修改的学生学号");
        String id = sc.next();
        System.out.println("请选择需要修改的内容1姓名2班级3数学成绩4英语成绩5计算机成绩");
        int n = sc.nextInt();
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            double average = 0;
            if (id.equals(s.getId())) {
                switch (n) {
                    case 1:
                        System.out.println("请输入修改后的内容");
                        String name = sc.next();
                        s.setName(name);
                        list.set(i, s);
                        init(list);
                    case 2:
                        System.out.println("请输入修改后的内容");
                        String classroom = sc.next();
                        s.setClassroom(classroom);
                        list.set(i, s);
                        init(list);
                    case 3:
                        System.out.println("请输入修改后的内容");
                        int math = sc.nextInt();
                        average = (math + s.getEnglish() + s.getComputer()) / 3.0;
                        s.setMath(math);
                        s.setAverage(Double.parseDouble(String.format("%.2f", average)));
                        list.set(i, s);
                        init(list);
                    case 4:
                        System.out.println("请输入修改后的内容");
                        int English = sc.nextInt();
                        average = (s.getMath() + English + s.getComputer()) / 3.0;
                        s.setEnglish(English);
                        s.setAverage(Double.parseDouble(String.format("%.2f", average)));
                        list.set(i, s);
                        init(list);
                    case 5:
                        System.out.println("请输入修改后的内容");
                        int Computer = sc.nextInt();
                        average = (s.getMath() + s.getEnglish() + Computer) / 3.0;
                        s.setComputer(Computer);
                        s.setAverage(Double.parseDouble(String.format("%.2f", average)));
                        list.set(i, s);
                        init(list);
                }
            }
        }
    }

    public static void search(ArrayList<Student> list) {
        System.out.println("请输入查询学生的学号或姓名");
        Scanner sc = new Scanner(System.in);
        String message = sc.next();
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (message.equals(s.getId()) || message.equals(s.getName())) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            Student s = list.get(index);
            System.out.println("学号"+"\t"+"姓名"+"\t"+"班级"+"\t"+"数学"+"\t"+"英语"+"\t"+"计算机"+"\t"+"平均分"+"\t");
            System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getClassroom() + "\t\t"
                    + s.getMath() + "\t\t" + s.getEnglish() + "\t\t" + s.getComputer() + "\t\t" + s.getAverage());
            init(list);
        } else {
            System.out.println("暂无此学生,请重新输入");
            search(list);
        }
    }

    public static void rank(ArrayList<Student> list) {
        System.out.println("请选择排序的方式1.平均成绩2.计算机成绩");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n == 1) {
            for (int j = 0; j < list.size() - 1; j++) {
                for (int i = 0; i < list.size() - 1 - j; i++) {
                    Student s1 = list.get(i);
                    Student s2 = list.get(i + 1);
                    if (s1.getAverage() > s2.getAverage()) {
                        list.set(i, s2);
                        list.set(i + 1, s1);
                    }
                }
            }
            System.out.println("学号"+"\t"+"姓名"+"\t"+"班级"+"\t"+"数学"+"\t"+"英语"+"\t"+"计算机"+"\t"+"平均分"+"\t");
            for (int i = list.size() - 1; i >= 0; i--) {
                Student s = list.get(i);
                System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getClassroom() + "\t\t"
                        + s.getMath() + "\t\t" + s.getEnglish() + "\t\t" + s.getComputer() + "\t\t" + s.getAverage());
            }

        } else if(n==2){
            for (int j = 0; j < list.size() - 1; j++) {
                for (int i = 0; i < list.size() - 1 - j; i++) {
                    Student s1 = list.get(i);
                    Student s2 = list.get(i + 1);
                    if (s1.getComputer() > s2.getComputer()) {
                        list.set(i, s2);
                        list.set(i + 1, s1);
                    }
                }
            }
            System.out.println("学号"+"\t"+"姓名"+"\t"+"班级"+"\t"+"数学"+"\t"+"英语"+"\t"+"计算机"+"\t"+"平均分"+"\t");
            for (int i = list.size() - 1; i >= 0; i--) {
                Student s = list.get(i);
                System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getClassroom() + "\t\t"
                        + s.getMath() + "\t\t" + s.getEnglish() + "\t\t" + s.getComputer() + "\t\t" + s.getAverage());
            }
        }else{
            System.out.println("输入错误，请重新输入");
            rank(list);
        }
        init(list);
    }

    public static void analyse(ArrayList<Student> list) {
        System.out.println("请选择查看的班级");
        Scanner sc = new Scanner(System.in);
        String classroom = sc.next();
        int CountStudent = 0;
        int SumMath = 0;
        int SumEnglish = 0;
        int SumComputer = 0;
        int MaxMath = 0;
        int MaxEnglish = 0;
        int MaxComputer = 0;
        int MinMath = 100;
        int MinEnglish = 100;
        int MinComputer = 100;
        int CountPassMath = 0;
        int CountPassEnglish = 0;
        int CountPassComputer = 0;
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (classroom.equals(s.getClassroom())) {
                SumMath += s.getMath();
                SumEnglish += s.getEnglish();
                SumComputer += s.getComputer();
                CountStudent++;
                if (s.getMath() > MaxMath) {
                    MaxMath = s.getMath();
                }
                if (s.getEnglish() > MaxEnglish) {
                    MaxEnglish = s.getEnglish();
                }
                if (s.getComputer() > MaxComputer) {
                    MaxComputer = s.getComputer();
                }
                if (s.getMath() < MinMath) {
                    MinMath = s.getMath();
                }
                if (s.getEnglish() < MinEnglish) {
                    MinEnglish = s.getEnglish();
                }
                if (s.getComputer() < MinComputer) {
                    MinComputer = s.getComputer();
                }
                if (s.getMath() >= 60) {
                    CountPassMath++;
                }
                if (s.getEnglish() >= 60) {
                    CountPassEnglish++;
                }
                if (s.getComputer() >= 60) {
                    CountPassComputer++;
                }
            }
        }
        if (CountStudent == 0) {
            System.out.println("班级输入错误，请重新输入");
            analyse(list);
        }
        double AveMath = Double.parseDouble(String.format("%.2f", (SumMath * 1.0) / CountStudent));
        double AveEnglish = Double.parseDouble(String.format("%.2f", (SumEnglish * 1.0) / CountStudent));
        double AveComputer = Double.parseDouble(String.format("%.2f", (SumComputer * 1.0) / CountStudent));
        double PassMath = Double.parseDouble(String.format("%.4f", (CountPassMath*1.0)/CountStudent))*100.0;
        double PassEnglish = Double.parseDouble(String.format("%.4f", (CountPassEnglish*1.0)/CountStudent))*100.0;
        double PassComputer = Double.parseDouble(String.format("%.4f", (CountPassComputer*1.0)/CountStudent))*100.0;
        System.out.println(classroom+"\t\t"+"平均成绩"+"\t"+"最高分"+"\t"+"最低分"+"\t"+"及格率");
        System.out.println("数学\t"+AveMath+"\t\t"+MaxMath+"\t\t"+MinMath+"\t\t"+PassMath+"%");
        System.out.println("英语\t"+AveEnglish+"\t\t"+MaxEnglish+"\t\t"+MinEnglish+"\t\t"+PassEnglish+"%");
        System.out.println("计算机\t"+AveComputer+"\t\t"+MaxComputer+"\t\t"+MinComputer+"\t\t"+PassComputer+"%");
        init(list);
    }

    public static void printStudent(ArrayList<Student> list) {
        System.out.println("学号"+"\t"+"姓名"+"\t"+"班级"+"\t"+"数学"+"\t"+"英语"+"\t"+"计算机"+"\t"+"平均分"+"\t");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getClassroom() + "\t\t"
                    + s.getMath() + "\t\t" + s.getEnglish() + "\t\t" + s.getComputer() + "\t\t" + s.getAverage());
        }
        init(list);
    }

    public static int getindex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (id.equals(s.getId())) {
                return i;
            }
        }
        return -1;
    }
}